from lxml import etree
import requests


def Response(url):
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44"
    }
    response = requests.get(url=url, headers=headers)
    return response


def GetCityData(response):
    html = response.text
    tree = etree.HTML(html)
    citys = tree.xpath('//ul[@class="city_table"]/li/a/text()')
    urls = tree.xpath('//ul[@class="city_table"]/li/a/@href')
    list_tuple = zip(citys, urls)
    return list_tuple


def GetRegionData(url, response):
    html = response.text
    tree = etree.HTML(html)
    regions = tree.xpath('//ul[@class="box"]/li[3]/a/text()')[1:]
    urls = tree.xpath('//ul[@class="box"]/li[3]/a/@href')[1:]
    urls = [url + i.split("/", 1)[1] for i in urls]
    list_tuple = zip(regions, urls)
    return list_tuple


def GetPageData(url, response):
    html = response.text
    tree = etree.HTML(html)
    titles = tree.xpath('//ul[@class="content_list_wrap"]/li/a/text()')
    urls = tree.xpath('//ul[@class="content_list_wrap"]/li/a/@href')
    urls = [url + i for i in urls]
    visits = tree.xpath('//div[@class="left visited"]/span/@title')
    visits = [int(i) for i in visits]
    dates = tree.xpath('//div[@class="left date"]/@title')
    list_tuple = zip(titles, urls, visits, dates)
    return list_tuple


def GetJanZhi(response):
    html = response.text
    tree = etree.HTML(html)
    job_type = tree.xpath('//a[@class="job_type"]/text()')
    job_type = job_type[0].strip() if job_type != [] else ""

    people = tree.xpath('//ul[@class="job_list"][1]/li[1]/span[2]/text()')
    people = int(people[0].strip()) if people != [] else ""
    job_address = tree.xpath('//ul[@class="job_list"][1]/li[2]/span[2]/text()')
    job_address = job_address[0].strip() if job_address != [] else ""

    time_ask = tree.xpath('//ul[@class="job_list"][2]/li[1]/span[2]/text()')
    time_ask = time_ask[0].strip() if time_ask != [] else ""
    job_kind = tree.xpath('//ul[@class="job_list"][2]/li[2]/span[2]/text()')
    job_kind = job_kind[0].strip() if job_kind != [] else ""
    working_time = tree.xpath('//ul[@class="job_list"][2]/li[3]/span[2]/text()')
    working_time = working_time[0].strip() if working_time != [] else ""

    settlement = tree.xpath('//ul[@class="job_list"][3]/li[1]/span[2]/text()')
    settlement = settlement[0].strip() if settlement != [] else ""
    wages = tree.xpath('//ul[@class="job_list"][3]/li[2]/span[2]/text()')
    wages = wages[0].strip() if wages != [] else ""
    if "万元/小时" in wages:
        wages = int(wages.strip("万元/小时").split(".")[0])
        wages = wages * 10000 * 8
    elif "千元/小时" in wages:
        wages = int(wages.strip("千元/小时").split(".")[0])
        wages = wages * 1000 * 8
    elif "元/小时" in wages:
        wages = int(wages.strip("元/小时").split(".")[0])
        wages = wages * 8
    elif "万元/天" in wages:
        wages = int(wages.strip("万元/天").split(".")[0])
        wages = wages * 10000
    elif "千元/天" in wages:
        wages = int(wages.strip("千元/天").split(".")[0])
        wages = wages * 1000
    elif "元/天" in wages:
        wages = int(wages.strip("元/天").split(".")[0])
    elif "万元/月" in wages:
        wages = int(wages.strip("万元/月").split(".")[0])
        wages = int((wages * 10000) / 30)
    elif "千元/月" in wages:
        wages = int(wages.strip("千元/月").split(".")[0])
        wages = int((wages * 1000) / 30)
    elif "元/月" in wages:
        wages = int(wages.strip("元/月").split(".")[0])
        wages = int(wages / 30)
    elif "万元/年" in wages:
        wages = int(wages.strip("万元/年").split(".")[0])
        wages = int((wages * 10000) / 365)
    elif "千元/年" in wages:
        wages = int(wages.strip("千元/年").split(".")[0])
        wages = int((wages * 1000) / 365)
    elif "元/年" in wages:
        wages = int(wages.strip("元/年").split(".")[0])
        wages = wages / 365
    else:
        wages = -1
    job_detailed = tree.xpath('//div[@id="job_detail"]/text()')
    job_detailed = job_detailed[0].strip().replace('"', "'") if job_detailed != [] else ""

    company = tree.xpath('//div[@class="company_info"]/a/text()')
    company = company[0].strip() if company != [] else ""
    company_introduction = tree.xpath('//div[@class="company_info"]/p[1]/text()')
    company_introduction = company_introduction[0].strip().replace('"', "'") if company_introduction != [] else ""
    company_address = tree.xpath('//div[@class="company_info"]/p[2]/text()')
    company_address = company_address[0].strip() if company_address != [] else ""

    # print(job_type)
    # print(people)
    # print(job_address)
    # print(time_ask)
    # print(job_kind)
    # print(working_time)
    # print(settlement)
    # print(wages)
    # print(job_detailed)
    # print(company)
    # print(company_introduction)
    # print(company_address)
    # print("-----------------------------------------------------------------")
    lists_tuple = [(job_type, people, job_address, time_ask, job_kind, working_time, settlement, wages, job_detailed,
                     company, company_introduction, company_address)]
    return lists_tuple



